package com.soft.mainsite.component.beetl.fun;

import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.module.web.cms.service.CategoryService;
import com.soft.mainsite.mybatis.model.TCmsCategory;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:content页面输出分类栏目
 *
 * @author Jin
 * @create 2017-04-15
 **/
@Service
public class ViewShowTreeCategoryFunction implements Function {

    @Autowired
    private CategoryService service;

    @Value("${system.http.protocol}")
    private String httpProtocol;

    @Value("${system.http.host}")
    private String httpHost;


    private String url = "/system/cms/category/input?id";

    @Override
    public Object call(Object[] objects, Context context) {

        Long pid = Long.parseLong(objects[0].toString());
        Integer siteId = Integer.parseInt(objects[1].toString());
        if (objects[2] != null && !StrUtil.isBlank(objects[2].toString())) {
            this.url = objects[2].toString();
        }
        return recursion(pid, siteId);
    }

    /**
     * 递归函数
     *
     * @param pid
     * @param siteId
     * @return
     */
    private String recursion(Long pid, Integer siteId) {
        StringBuffer sbf = new StringBuffer();
        List<TCmsCategory> cats = service.findCategoryListByPid(pid, siteId);
        if (cats != null && cats.size() > 0) {
            for (TCmsCategory cat : cats) {
                int childSize = service.findCategoryListByPid(cat.getCategoryId(), siteId).size();
                if ((cat.getAlone() || (!StrUtil.isBlank(cat.getUrl())) && childSize == 0)) {
                    continue;
                } else if (childSize > 0) {
                    sbf.append("  <li data-id=\"" + cat.getCategoryId() + "\" data-pid=\"" + pid + "\" >" + cat.getCategoryName() + " [" + cat.getCategoryId() + "] </li>");
                } else {
                    sbf.append("  <li data-id=\"" + cat.getCategoryId() + "\" data-pid=\"" + pid + "\" data-url=\"" + httpProtocol + "://" + httpHost + url + "=" + cat.getCategoryId() + "\" data-divid=\"#layout-content-view-show\">" + cat.getCategoryName() + " [" + cat.getCategoryId() + "] </li>");
                }
                sbf.append(recursion(cat.getCategoryId(), siteId));
            }
            return sbf.toString();
        }
        return "";
    }
}
